<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>对象继承</title>
    <script>
      // 原型式继承
      function Person(name, age) {
        this.name = name;
        this.age = age;
        this.personInfo = function () {
          return this.name + this.age;
        };
      }
      Person.prototype.type = "人类";

      log("有参数函数继承");
      function Student(name, age, classRoom) {
        Person.call(this, name, age, classRoom);
        this.classRoom = classRoom;
        this.studentInfo = function () {
          return this.name + this.age + this.classRoom;
        };
      }

      student1 = new Student(" dong ", 18, " Three class ");
      console.log("studentInfo: ", student1.studentInfo());
      console.log("personInfo: ", student1.personInfo());

      log("无参数函数继承");
      function Brick(name, age) {
        // 无参数情况下, 无法设置父级属性
        Person.call(this);
        this.brickInfo = () => {
          return name + age;
        };
      }
      student2 = new Brick(" dong ", 18);
      console.log("brickInfo: ", student2.brickInfo());
      console.log("personInfo: ", student2.personInfo());

      log("默认情况下原型继承不继承原型构造函数");
      console.log("Student.prototype.type: ", Student.prototype.type);
      log("设置原型和构造器引用(prototype)");
      Student.prototype = Object.create(Person.prototype);
      console.log("Student.prototype.type: ", Student.prototype.type);

      function log(msg) {
        console.log("----------------" + msg + "----------------");
      }
    </script>
  </head>
  <body></body>
</html>
